<?php

namespace app\index\model;

use think\Loader;
use think\Model;
use think\Session;

class Userlogin extends Model
{
    protected $table = 'user';


    public function login($data)
    {
        //1.执行验证
        $validate = Loader::validate('Userlogin');

        if (!$validate->check($data)) {
            //
            return ['valid' => 0, 'msg' => $validate->getError()];
        }
        //2.对比用户名是否正确
        $res = $this->where('username', $data['username'])->where('password', md5(md5($data['password'])))->find();
        if (!$res) {
            return ['valid' => 0, 'msg' => '用户名或密码不正确,请重新输入'];
        }
        //3.将用户的信息存入到session中
        Session::set('user.username', $data['username']);
        Session::set('user.uid', $res['uid']);
//        Session::set('user.uid', $data['username']);
        return ['valid' => 1, 'msg' => '恭喜您,登录成功'];
    }



}
